package com.yuanluludev.lib_xpopup

import android.content.Context
import android.view.View
import com.yuanluludev.lib_xpopup.core.AttachPopupView
import com.yuanluludev.lib_xpopup.enums.PopupAnimation
import com.yuanluludev.lib_xpopup.interfaces.OnCancelListener
import com.yuanluludev.lib_xpopup.interfaces.OnConfirmListener
import com.yuanluludev.lib_xpopup.interfaces.OnInputConfirmListener
import com.yuanluludev.lib_xpopup.interfaces.OnSelectListener

/**
 * yuanLulu
 * 通用弹窗
 */
object CommonDialogUtils {

    /**
     * 通用 确认取消弹窗
     */
    fun showConfirmDialog(
        context: Context,
        title: String = "提示",
        content: String = "这是内容",
        cancelBtnText: String = "取消",
        confirmBtnText: String = "确定",
        isHideCancel: Boolean = false,
        hasShadowBg: Boolean = true,
        isDismissOnTouchOutside: Boolean = true,
        isDismissOnBackPressed: Boolean = true,
        isDestroyOnDismiss: Boolean = true,
        popupAnimation: PopupAnimation = PopupAnimation.ScaleAlphaFromCenter,
        onConfirmListener: OnConfirmListener? = null,
        onCancelListener: OnCancelListener? = null
    ) {
        XPopup.Builder(context)
            .hasShadowBg(hasShadowBg) // 是否有半透明的背景，默认为true
            .dismissOnTouchOutside(isDismissOnTouchOutside) //点击外部是否关闭弹窗，默认为true
            .dismissOnBackPressed(isDismissOnBackPressed) //按返回键是否关闭弹窗，默认为true
            .isDestroyOnDismiss(isDestroyOnDismiss)
            .popupAnimation(popupAnimation)
            .asConfirm(
                title,
                content,
                cancelBtnText,
                confirmBtnText,
                onConfirmListener,
                onCancelListener,
                isHideCancel
            )
            .show()
    }

    /**
     * 显示带有输入框，确认和取消对话框
     *
     * @param title           对话框标题，传空串会隐藏标题
     * @param content         对话框内容,，传空串会隐藏
     * @param inputContent    输入框文字内容，会覆盖hint
     * @param hint            输入框默认文字
     * @param confirmListener 点击确认的监听器
     * @param cancelListener  点击取消的监听器
     */
    fun showInputDialog(
        context: Context,
        title: String = "提示",
        content: String = "这是内容",
        inputContent: String = "",
        hint: String = "",
        confirmListener: OnInputConfirmListener? = null
    ) {
        XPopup.Builder(context)
            .asInputConfirm(title, content, inputContent, hint, confirmListener)
            .show()
    }

    /**
     * 依附于某个View
     */
    fun showAttachPop(
        context: Context,
        v: View,
        arrString: Array<String>,
        arrInt: IntArray? = null,
        listener: OnSelectListener,
    ): AttachPopupView {
        val attachPopupView =
            XPopup.Builder(context)
                .hasShadowBg(true)
                .isCoverSoftInput(true)
                //                        .isViewMode(true)
                //                        .isRequestFocus(false)
                //                        .popupAnimation(PopupAnimation.ScrollAlphaFromTop)
                //                        .isClickThrough(true)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                //                        .isDarkTheme(true)
                //                        .popupAnimation(PopupAnimation.ScrollAlphaFromTop) //NoAnimation表示禁用动画
                //                        .isCenterHorizontal(true) //是否与目标水平居中对齐
                //                        .offsetY(60)
                //                        .offsetX(80)
                //                        .popupPosition(PopupPosition.Top) //手动指定弹窗的位置
                //                        .popupWidth(500)
                .atView(v) // 依附于所点击的View，内部会自动判断在上方或者下方显示
                .asAttachList(arrString, arrInt, listener, 0, 0)
        attachPopupView.show()
        return attachPopupView
    }

    /**
     * 带选中的底部弹出列表弹窗
     */
    fun showBottomListPop(
        context: Context,
        title: String = "请选择",
        array: Array<String>,
        checkPos: Int = -1,
        listener: OnSelectListener,
    ) {
        XPopup.Builder(context)
            //对于只使用一次的弹窗，推荐设置这个
            .isDestroyOnDismiss(true)
            .asBottomList(
                title,
                array,
                null,
                checkPos,
                listener
            )
            .show()
    }


    /**
     * 在中间弹出的List列表弹窗，带选中效果
     */
    fun showCenterListWithCheckPop(
        context: Context,
        title: String = "请选择",
        array: Array<String>,
        checkPos: Int = -1,
        listener: OnSelectListener,
    ) {
        XPopup.Builder((context))
            //对于只使用一次的弹窗，推荐设置这个
            .isDestroyOnDismiss(true)
            .asCenterList(
                title,
                array,
                null,
                checkPos,
                listener
            )
            .show()
    }

}